package com.properties.test.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.properties.test.vo.UserInfo;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "data.test")
@Data
@ToString
public class DemoProperties {
    private String name;
    private Integer age;
    private List<String> idNos;
    private List<UserInfo> userInfos;
    private Map<String,ExtAddress> extMap;

    /**
     * 重写set方法，将List<Map>转换成目标类型
     * 可以使用序列化或者反射技术
     * @param userInfosMap
     */
    public void setUserInfos(List<Map<String,Object>> userInfosMap){
        ObjectMapper mapper = new ObjectMapper();
        try {
            userInfos = mapper.readValue(mapper.writeValueAsString(userInfosMap),new TypeReference<List<UserInfo>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
